/*
 * Copyright (c) 2023.
 *
 * 作者：bmy
 * 邮箱：2271608011@qq.com
 * gitee：https://gitee.com/bmycode
 * github：https://github.com/helpcode
 *
 */
import Http from "@/http";
import config from "@/config";
import type { ReleasesLatestType } from "@/serve/types/ReleasesLatestType";

class Gitee {

  public async ReleasesLatest(): Promise<ReleasesLatestType> {
    return await Http.get(config.giteeApi.releasesUrl)
  }


  /**
   *
   * @param curV 新版本号
   * @param reqV 当前应用的版本号
   */
  public compare(curV: string, reqV: string): boolean {
    if (curV && reqV) {
      //将两个版本号拆成数字
      let arr1 = curV.split('.'),
        arr2 = reqV.split('.');
      let minLength = Math.min(arr1.length, arr2.length),
        position = 0,
        diff = 0;
      //依次比较版本号每一位大小，当对比得出结果后跳出循环（后文有简单介绍）
      while (position < minLength && ((diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0)) {
        position++;
      }
      diff = (diff != 0) ? diff : (arr1.length - arr2.length);
      //若curV大于reqV，则返回true
      return diff > 0;
    } else {
      //输入为空
      return false;
    }
  }
}

export default new Gitee